
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/index/iview/betting_result_iview.dart';
import 'package:flutter_deer/index/models/user_betting_entity.dart';
import 'package:flutter_deer/index/presenter/betting_result_presenter.dart';
import 'package:flutter_deer/index/provider/betting_result_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/image_utils.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/load_image.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:text_scroll/text_scroll.dart';

class BettingResultPage extends StatefulWidget {

  const BettingResultPage({
    super.key,
    this.bettingId
  });

  final String? bettingId;

  @override
  _BettingResultPageState createState() => _BettingResultPageState();
}

class _BettingResultPageState extends State<BettingResultPage> with BasePageMixin<BettingResultPage, PowerPresenter>, AutomaticKeepAliveClientMixin<BettingResultPage> implements BettingResultIMvpView {
  @override
  bool get wantKeepAlive => false;
  late BettingResultPagePresenter bettingResultPagePresenter;
  BettingResultPageProvider provider = BettingResultPageProvider();

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(
        this);
    bettingResultPagePresenter =
    new BettingResultPagePresenter();
    powerPresenter.requestPresenter([bettingResultPagePresenter]);
    return powerPresenter;
  }

  @override
  void setBettingResult(UserBettingResultEntity? result){
    provider.setBettingResultEntity(result);
  }

  @override
  void initState() {
    super.initState();
    bettingResultPagePresenter.getBettingDetail(widget.bettingId);
  }

  @override
  void dispose() {
    super.dispose();
    provider.dispose();
  }

  Future<dynamic> _onRefresh() async {
    bettingResultPagePresenter.getBettingDetail(widget.bettingId);
  }

  Future<dynamic> _loadMore() async {
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<BettingResultPageProvider>(
        create: (_) => provider,
        child: Consumer<BettingResultPageProvider>(
        builder: (_,provider,child) {
          if (provider.stateType == StateType.loading) {
            return Scaffold(
                appBar: MyAppBar(
                    centerTitle: '${DeerLocalizations.of(context)!.dataLoading.nullSafe}'
                )
            );
          } else {
            return Scaffold(
              appBar: MyAppBar(
                  centerTitle: '${DeerLocalizations.of(context)!.bettingDetail.nullSafe}'
              ),
              body: DeerListView(
                  itemCount: provider.gameLength! + 1,
                  stateType: provider.stateType!,
                  onRefresh: _onRefresh,
                  loadMore: _loadMore,
                  hasMore: false,
                  itemBuilder: (_, index) {
                    if (index == provider.gameLength) {
                      TextStyle textStyle = TextStyle(color: context.isDark?Colours.dark_text:Colours.text);
                      if(provider.resultEntity!.status.nullSafe=='1'){
                        textStyle =  TextStyle(color: Colors.red);
                      }else if(provider.resultEntity!.status.nullSafe=='2'){
                        textStyle =  TextStyle(color: Colors.green);
                      }
                      return Padding(
                          padding: EdgeInsets.symmetric(
                              horizontal: 10.0, vertical: 5),
                          child: MyCard(
                              child: Padding(
                                padding: EdgeInsets.symmetric(horizontal: 5),
                                child: Column(
                                  children: [
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.playWays.nullSafe}'),
                                        Text(provider.resultEntity!.playWay
                                            .nullSafe)
                                      ],
                                    ),
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.bettingTime.nullSafe}'),
                                        Text(provider.resultEntity!.bettingTime
                                            .nullSafe)
                                      ],
                                    ),
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.bettingAmount.nullSafe}'),
                                        Text(NumberFormat.decimalPattern('en_us').format(provider.resultEntity!.bettingScore)
                                            .toString())
                                      ],
                                    ),
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.odds.nullSafe}'),
                                        Text(provider.resultEntity!.ratio
                                            .toString())
                                      ],
                                    ),
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.willPrizeAmount.nullSafe}'),
                                        Text(NumberFormat.decimalPattern('en_us').format(provider.resultEntity!.willPrizeAmount).toString())
                                      ],
                                    ),
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.bettingType.nullSafe}'),
                                        Text(provider.resultEntity!
                                            .useScoreType == '1'
                                            ? '${DeerLocalizations.of(context)!.recharge.nullSafe}'
                                            : '${DeerLocalizations.of(context)!.give.nullSafe}')
                                      ],
                                    ),
                                    Gaps.vGap5,
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.lotteryStatus.nullSafe}'),
                                        Text(provider.resultEntity!.bettingStatus.nullSafe,style: textStyle,)
                                      ],
                                    ),
                                    Visibility(visible: provider.resultEntity!.status=='1',child: Gaps.vGap5,),
                                    Visibility(visible: provider.resultEntity!.status=='1',child:  Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.winningAmount.nullSafe}'),
                                        Text(NumberFormat.decimalPattern('en_us').format(provider.resultEntity!.winScore).toString(),style: textStyle,)
                                      ],
                                    ),),
                                    Visibility(visible: provider.resultEntity!.status=='2',child:  Row(
                                      mainAxisAlignment: MainAxisAlignment
                                          .spaceBetween,
                                      children: [
                                        Text('${DeerLocalizations.of(context)!.loss.nullSafe}'),
                                        Text(NumberFormat.decimalPattern('en_us').format(provider.resultEntity!.bettingScore).toString(),style: textStyle,)
                                      ],
                                    ),),
                                    Gaps.vGap8,
                                  ],
                                ),
                              )
                          )
                      );
                    } else {
                      UserBettingDetailEntity detailEntity = provider
                          .resultEntity!.details!.elementAt(index);
                      return Padding(
                        padding: EdgeInsets.symmetric(
                            horizontal: 10.0, vertical: 5),
                        child: MyCard(
                          child: Padding(
                            padding: EdgeInsets.symmetric(horizontal: 5),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment
                                      .spaceBetween,
                                  children: [
                                    Text(detailEntity.gameTitle.nullSafe),
                                    Text(detailEntity.playDate.nullSafe.substring(5) + ' ' +
                                        detailEntity.playTime.nullSafe)
                                  ],
                                ),
                                Gaps.vGap5,
                                Divider(height: 1.0, indent: 0.0),
                                Gaps.vGap5,
                                Row(
                                  mainAxisAlignment: MainAxisAlignment
                                      .spaceAround,
                                  children: [
                                    Container(
                                      width: (MediaQuery.of(context).size.width-80)/2,
                                      child: Column(
                                        children: [
                                          Image(
                                            image: ImageUtils.getImageProvider(
                                                Constant.imageServerHost +
                                                    '${detailEntity
                                                        .teamOneCover}',
                                                holderImg: 'shop/tx'),
                                            width: 30,
                                            height: 30,),
                                          SizedBox(height: 12,),
                                          TextScroll(detailEntity.teamOne.nullSafe,mode: TextScrollMode.endless,pauseBetween: Duration(milliseconds: 500),)
                                        ],
                                      ),
                                    ),
                                    LoadAssetImage(
                                        'logo', format: ImageFormat.gif,
                                        width: 50,
                                        height: 50),
                                    Container(
                                      width: (MediaQuery.of(context).size.width-80)/2,
                                      child: Column(
                                        children: [
                                          Image(
                                            image: ImageUtils.getImageProvider(
                                                Constant.imageServerHost +
                                                    '${detailEntity
                                                        .teamTwoCover}',
                                                holderImg: 'shop/tx'),
                                            width: 30,
                                            height: 30,),
                                          SizedBox(height: 12,),
                                          TextScroll(detailEntity.teamTwo.nullSafe,mode: TextScrollMode.endless,pauseBetween: Duration(milliseconds: 500),)
                                        ],
                                      )
                                    ),
                                  ],
                                ),
                                Gaps.vGap8,
                                Divider(height: 1.0, indent: 0.0),
                                Gaps.vGap5,
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                                  children: [
                                    Visibility(
                                      visible: provider.resultEntity!.bettingType == '1',
                                      child: Text('${DeerLocalizations.of(context)!.score.nullSafe}:${detailEntity.score.nullSafe}',textAlign: TextAlign.center,),),
                                    Visibility(
                                      visible: provider.resultEntity!.bettingType == '2',
                                      child: Text(
                                          '${DeerLocalizations.of(context)!.winOrLose.nullSafe}:${detailEntity.outcome.nullSafe}',textAlign: TextAlign.center),),
                                    Visibility(
                                      visible: provider.resultEntity!.bettingType == '3',
                                      child: Text(
                                          '${DeerLocalizations.of(context)!.numberOfGoalsScored.nullSafe}:${detailEntity.minBallNum.toString()}-${detailEntity.maxBallNum.toString()}',textAlign: TextAlign.center),),
                                    Visibility(
                                        visible: detailEntity.letBallNum! > 0,
                                        child: Text(
                                            '${DeerLocalizations.of(context)!.handicap.nullSafe}:${detailEntity.letBallNum.toString()}',textAlign: TextAlign.center)),
                                    Text('${DeerLocalizations.of(context)!.odds.nullSafe}:${detailEntity.odds}'),

                                    Visibility(visible: detailEntity.competitionResult!='',child: Text('${DeerLocalizations.of(context)!.gameResult.nullSafe}:${detailEntity.competitionResult}',textAlign: TextAlign.center),)
                                  ],
                                ),
                                Gaps.vGap8,
                              ],
                            ),
                          ),
                        ),
                      );
                    }
                  }
              ),

              /// 同时存在底部按钮与keyboardConfig配置时，为保证Android与iOS平台软键盘弹出高度正常，添加下面的代码。
              resizeToAvoidBottomInset: defaultTargetPlatform !=
                  TargetPlatform.iOS,
            );
          }
        }));
  }
}